//添加多个参数注意使用[]
layui.use(['form', 'jquery', 'upload'], function () {

    const form = layui.form,
        $ = layui.jquery,
        upload = layui.upload;

    let code = localStorage.getItem("loginUserCode");
    findCodeByType('degree', $('#degree'));
    findByCode(code);
    //显示日期
    showTime('#birthday');
    showTime('#joinTime');
    /**
     * 图片上传
     */
    upload.render({
        elem: '#avatarImg',//绑定元素
        headers:HEADERS,
        url: LOCAL_CROSS_URL + '/upload/img', //请求接口
        done: function (reg) {
            //上传成功回调
            if (reg.code === 200) {
                $('#priview').attr('src', reg.data);
                $('#avatar').val(reg.data);
                layer.msg("上传成功", {icon: 1, time: 1000, shade: 0.4})
            } else {
                layer.msg("上传失败", {icon: 5, time: 1000, shade: 0.4})
            }
        },
        error: function () {
            //请求异常回调
        }
        , size: 1024 * 5 //最大5M
    });


    /**
     * 查询单个
     * @param code
     */
    function findByCode(code) {

        let result = commonAjax('/user/findByCode', {code: code}, 'get');
        setData(result.data);
    }

    /**
     * 赋值
     */
    function setData(data) {
        $('#id').val(data.id);
        $('#code').val(data.code);
        $('#username').val(data.username);
        $('#stuId').val(data.stuId);
        $('#nickname').val(data.nickname);
        $('#degree').val(data.degree);
        $("input[name='gender'][value='" + data.gender + "']").next().click();
        $('#phone').val(data.phone);
        $('#age').val(data.age);
        $('#birthday').val(data.birthday);
        $('#classes').val(data.classes);
        $('#joinTime').val(data.joinTime);
        $('#joinFrequency').val(data.joinFrequency);
        $('#classRoom').val(data.classRoom);
        $('#position').val(data.position);
        //显示图片
        $('#priview').attr('src', data.avatar);
        $('#avatar').val(data.avatar);

        form.render();
    }

    /**
     * 保存
     */
    form.on('submit(commit)', function (data) {

        let formData = JSON.stringify(data.field);
        let result = commonAjax('/user/update', formData, 'post');
        if (result.code === 200) {
            layer.msg("保存成功！", {icon: 1, time: 2000, shade: 0.4});
        } else {
            layerMsgSaveFail();
        }
        return false;
    });

});